﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ajax_image : Base
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected override void Render(HtmlTextWriter writer)
    {
        Model.User user = Session["user"] as Model.User;
        string type = base.Query("type");
        int id = base.QueryInt32("id");

        string value = "";
        switch (type)
        {
            case "love": value = Love(id); break;
            case "shou": value = Shoucang(id, user); break;
            case "jubao": value = Jubao(user); break;
            case "liuyan": value = Liuyan(id, user); break;
        }
        Response.Write(value);
    }


    public string Love(int id)
    {
        DAL.Img dal = new DAL.Img();
        if (dal.ExecuteSql(" update  TB_Img set LoveNum=LoveNum+1 where ID = " + id) > 0)
        {
            return "1";
        }
        else return "0";
    }
    public string Shoucang(int id, Model.User user)
    {
        if (Session["user"] == null)
        {
            return "";
        }
        else
        {
            BLL.FavoritesImg bll = new BLL.FavoritesImg();
            string sql = string.Format(" UserID={0} and  ImgID={1}", user.ID, id);
            if (!bll.Exists(sql))
            {
                Model.FavoritesImg img = new Model.FavoritesImg();
                img.UserID = user.ID;
                img.ImgID = id;
                img.AddTime = System.DateTime.Now;
                if (bll.Add(img) > 0)
                {
                    return "1";
                }
                else return "0";
            }
            else
                return "1";
        }
    }
    public string Jubao(Model.User user)
    {
        string url = base.Query("url");
        if (Session["user"] == null)
        {
            return "";
        }
        else
        {
            Model.Report re = new Model.Report();
            re.UserID = user.ID;
            re.Url = url;
            re.IsProcessing = false;
            re.AddTime = System.DateTime.Now;
            if (new BLL.Report().Add(re) > 0)
            {
                return "1";
            }
            else return "0";
        }
    }
    public string Liuyan(int id, Model.User user)
    {
        if (Session["user"] == null)
        {
            return "";
        }
        else
        {
            int pid = base.QueryInt32("parent");
            Model.CommentImg img = new Model.CommentImg();
            img.ImgID = id;
            img.UserID = user.ID;
            img.ParentID = pid;
            img.Content = base.Query("comment");
            img.AddTime = System.DateTime.Now;
            if (new BLL.CommentImg().Add(img) > 0)
            {
                return "1";
            }
            else return "0";
        }
    }
}
